//
//  addanddelTableViewController.m
//  11210
//
//  Created by student on 16/3/4.
//  Copyright © 2016年 niit. All rights reserved.
//

#import "addanddelTableViewController.h"

@interface addanddelTableViewController ()
{
    BOOL bAdded;
}
@property (nonatomic,strong) NSMutableArray *list;

@end

@implementation addanddelTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.list = [@[@"张三",@"李四",@"王五"] mutableCopy];
    UIBarButtonItem *movebtn = [[UIBarButtonItem alloc]initWithTitle:@"编辑" style:UIBarButtonItemStylePlain target:self action:@selector(toggleMove)];
    self.navigationItem.rightBarButtonItem = movebtn;
   }

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
  }

- (void)toggleMove
{
    [self.tableView setEditing:!self.tableView.editing animated:YES];
    [self.navigationItem.rightBarButtonItem setTitle:self.tableView.editing?@"完成":@"编辑"];
    if (self.tableView.editing && !bAdded)
    {
        bAdded = YES;
        NSIndexPath *tmpPath = [NSIndexPath indexPathForRow:self.list.count inSection:0];
        [self.tableView insertRowsAtIndexPaths:@[tmpPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    if (!self.tableView.editing && bAdded)
    {
        bAdded = NO;
        NSIndexPath *tmpPath = [NSIndexPath indexPathForRow:self.list.count inSection:0];
        [self.tableView deleteRowsAtIndexPaths:@[tmpPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (bAdded)
    {
        return self.list.count+1;
    }
    else
    {
        return self.list.count;
    }
    
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    
    if (indexPath.row == self.list.count)
    {
        cell.textLabel.text = @"添加新同学";
    }
    else
    {
        cell.textLabel.text = self.list[indexPath.row];
    }
    
    return cell;
}

- (UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == self.list.count)
    {
        return UITableViewCellEditingStyleInsert;
    }
    else
    {
        return UITableViewCellEditingStyleDelete;
    }
}

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle == UITableViewCellEditingStyleDelete)
    {
        [self.list removeObjectAtIndex:indexPath.row];
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
    
    if (editingStyle == UITableViewCellEditingStyleInsert)
    {
        [self.list addObject:[NSString stringWithFormat:@"新同学%lu",(unsigned long)self.list.count]];
        [tableView insertRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    }
}

@end
